package com.azxcrm.project.monitor.job.task;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.azxcrm.common.utils.DateUtils;
import com.azxcrm.project.system.basic.domain.OrderDispatch;
import com.azxcrm.project.system.basic.domain.OrderModifyRecord;
import com.azxcrm.project.system.basic.service.IOrderDispatchService;
import com.azxcrm.project.system.basic.service.IOrderModifyRecordService;
import com.azxcrm.project.system.customer.dao.ICustomerAttendanceDao;
import com.azxcrm.project.system.customer.domain.CustomerAttendance;
import com.azxcrm.project.system.order.domain.Order;
import com.azxcrm.project.system.order.service.IOrderService;

/**
 * 定时器任务查询参课记录
 * @author Dustyone
 *
 */
@Component("attendance")
public class Attendance {

	/**
	 * 参课信息
	 */
	@Autowired
	private ICustomerAttendanceDao customerAttendanceDao;
	
	/**
	 * 派单记录
	 */
	@Autowired
	private IOrderDispatchService orderDispatchService;
	
	/**
	 * 订单操作记录
	 */
	@Autowired
	private IOrderModifyRecordService orderModifyRecordService;
	
	/**
	 * 订单信息
	 * @throws ParseException
	 */
	@Autowired
	private IOrderService orderService;
	
	public void attendanceRecord() throws ParseException {
		String currentDate = DateUtils.getDate();//当前时间
		//派单记录
		OrderDispatch orderDispatch = new OrderDispatch();
		//定时查询新增参课记录表中的数据
		List<CustomerAttendance> customerAttendance = customerAttendanceDao.selectCustomerAttendanceTiming();
		//判断参课记录表中有无数据
		if(null != customerAttendance) {
			//循环遍历参课表中数据
			for(CustomerAttendance dance : customerAttendance) {
				//判断订单类型
				if(dance.getOrder().getOrderStatus()<5 && dance.getOrder().getOrderStatus()>1) {
					//订单信息
					Order order = orderService.selectOrderById(dance.getOrder().getOrderId());
					//派单记录派单保护类型为跟单保护
					if(dance.getOrderDispatch().getProtectType()!=1) {
						String danceDate = DateUtils.plusDay(30,dance.getAttendanceTime());//跟单保护时间 = 参课时间+30天
						//判断当前时间是否已经超过参课保护时间
						if(DateUtils.compare_date(currentDate, dance.getOrderDispatch().getProtectTime())) {
							order.setOrderStatus(0);
							orderService.updateOrder(order);
							OrderModifyRecord modify = new OrderModifyRecord();
							modify.setOrderId(order.getOrderId());
							modify.setModifyUserId(1);
							modify.setModifyType(1);
							modify.setModifyRecod("超过参课保护时间");
							orderModifyRecordService.saveOrderModifyRecord(modify);
						}else {
							orderDispatch.setDispatchId(dance.getOrderDispatch().getDispatchId());
							orderDispatch.setProtectType(1);
							orderDispatch.setProtectTime(danceDate);
							orderDispatchService.updateOrderDispatch(orderDispatch);
						}
					}
				}
			}
		}
		//获取所有派单记录表中参课保护状态下的保护时间
		List<OrderDispatch> ords = orderDispatchService.selectAllProtectTime();
		Order order = new Order();
		//遍历获取参课保护时间
		for(OrderDispatch ord : ords) {
			if(DateUtils.compare_date(currentDate, ord.getProtectTime())) {
				order.setOrderId(ord.getOrderId());
				order.setOrderStatus(0);
				orderService.updateOrder(order);
				OrderModifyRecord modify = new OrderModifyRecord();
				modify.setOrderId(order.getOrderId());
				modify.setModifyUserId(1);
				modify.setModifyType(1);
				modify.setModifyRecod("超过参课保护时间");
				orderModifyRecordService.saveOrderModifyRecord(modify);
			}
		}
		
	}
	
	public void attendanceDocumentary() throws ParseException {
		//定时查询新增参课记录表中的数据
		List<CustomerAttendance> customerAttendance = customerAttendanceDao.selectCustomerAttendanceTiming();
		//判断参课记录表中有无数据
		if(null != customerAttendance) {
			//循环遍历参课表中数据
			for(CustomerAttendance dance : customerAttendance) {
				//判断订单类型
				if(dance.getOrder().getOrderStatus()<5 && dance.getOrder().getOrderStatus()>1) {
					//订单信息
					Order order = orderService.selectOrderById(dance.getOrder().getOrderId());
					//派单记录派单保护类型为跟单保护
					if(dance.getOrderDispatch().getProtectType() == 1) {
						String currentDate = DateUtils.getDate();//当前时间
						//判断当前时间是否已经超过跟单保护时间
						if(DateUtils.compare_date(currentDate, dance.getOrderDispatch().getProtectTime())) {
							order.setOrderStatus(0);
							orderService.updateOrder(order);
							OrderModifyRecord modify = new OrderModifyRecord();
							modify.setOrderId(order.getOrderId());
							modify.setModifyUserId(1);
							modify.setModifyType(1);
							modify.setModifyRecod("超过跟单保护时间");
							orderModifyRecordService.saveOrderModifyRecord(modify);
						}
					}
				}
			}
		}
	}
	
	
}
